package org.common.utils.fasttest;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;

import java.util.*;
import java.util.function.Function;

/**
 * @author zhouzhibing
 * @date 2024/4/7
 */
public class TestTmp {


    private static Integer number = 10;
    private static final Function<Object , Object> FUN = (o) ->{
        return o;
    };

    public static void test() {

        long testCount = 1000000000L;
        int roundCount = 10;

//        test1(testCount , roundCount);
        test2(testCount , roundCount);
//        test3(testCount , roundCount);
    }

    private static void test3(long testCount, int roundCount) {
        //多文件进行合并
        JSONArray jsonArray = new JSONArray();

//        String fileContent = "{\"strength\":5,\"agility\":5,\"stamina\":5,\"intelligence\":5,\"dexterity\":5,\"luck\":5}";
        String fileContent = "[{\"strength\":5,\"agility\":5,\"stamina\":5,\"intelligence\":5,\"dexterity\":5,\"luck\":5},{\"strength\":5,\"agility\":5,\"stamina\":5,\"intelligence\":5,\"dexterity\":5,\"luck\":5}]";
        if(fileContent == null || fileContent.isEmpty()) {
            return;
        }

        String head = fileContent.substring(0 , 1);
        String tail = fileContent.substring(fileContent.length() - 1);
        if(head.equals("{") && tail.equals("}")) {
            jsonArray.add(JSONObject.parseObject(fileContent));
        } else {
            jsonArray.addAll(JSONArray.parse(fileContent));
        }
        String content = jsonArray.toJSONString();
        System.out.println("content = " + content);
    }

    private static void test2(long testCount, int roundCount) {
        Integer number = 10;
//        Test.doHandler("functionCallback" , testCount , roundCount , () ->{
//            functionCallback(number, (o) ->{
//                return number;
//            });
//        });

        Test.doHandler("doMethod" , testCount , roundCount , (index) ->{
            functionCallback(number,  TestTmp::doMethod);
        });

//        Test.doHandler("doMethod" , testCount , roundCount , () ->{
//            doMethod(number);
//        });

//
//        Test.doHandler("FUN" , testCount , roundCount , () ->{
//            functionCallback(FUN);
//        });
    }


    private static <T> T functionCallback(int number, Function<Object , T> function) {
        return function.apply(number);
    }

    private static <T> T doMethod(Object o) {
        return (T)o;
    }

    private static  void test1(int testCount , int roundCount) {
        List<Integer> list = new ArrayList<>();

        List<Integer> temp = new ArrayList<>();
        temp.add(1);

        Test.doHandler("add" , 1 , roundCount , (index) ->{
            for (int i = 0; i < testCount; i++) {
                list.add(i);
            }
            list.clear();
        });

        Test.doHandler("addAll" , 1 , roundCount , (index) ->{
            for (int i = 0; i < testCount; i++) {
                list.addAll(temp);
            }
            list.clear();
        });
    }

}
